package ccc.hqu.edu.cn.whatmaps;

import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.mapapi.cloud.CloudRgcResult;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiDetailSearchResult;
import com.baidu.mapapi.search.poi.PoiIndoorResult;
import com.baidu.mapapi.search.poi.PoiNearbySearchOption;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;

import java.util.ArrayList;
import java.util.List;

import static android.text.TextUtils.*;

public class SearchActivity extends AppCompatActivity {
    private SearchView mSearchView;
    private ListView mListView;
    private PoiSearch mPoiSearch;
    private List<com.baidu.mapapi.search.core.PoiInfo> nearList;
    private ListAdapter adapter;
    private int pageindex = 0;
    double[] locationre;
    private String locationName;
    private String searchInfo = null;
    private int dis = 100;
    int pagenum = 0;

    Button prv;
    Button next;
    TextView page;
    SharedPreferences preferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        mSearchView = findViewById(R.id.searchView);
        mListView = findViewById(R.id.resultlist);
        prv = findViewById(R.id.serach_prv);
        next = findViewById(R.id.serach_next);
        page = findViewById(R.id.search_page);
        Intent intent = getIntent();
        mPoiSearch = PoiSearch.newInstance();
        mPoiSearch.setOnGetPoiSearchResultListener(listener);
        nearList = new ArrayList();
        preferences = PreferenceManager.getDefaultSharedPreferences(this);
        switch (preferences.getString("dis","100米")){
            case "100米":dis = 100;break;
            case "500米":dis = 500;break;
            case "1千米":dis = 1000;break;
            case "5千米":dis = 5000;break;
            case "10千米":dis = 10000;break;
            default:dis = 100;
        }
        mSearchView.setIconifiedByDefault(false);
        final double location[] = intent.getDoubleArrayExtra("myloc");//获取位置坐标，请用Double[]传入。
        final LatLng loc = new LatLng(location[0],location[1]);


        mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                if(!isEmpty(query)){
                    searchInfo = query;
                    mPoiSearch.searchNearby(new PoiNearbySearchOption()
                            .location(loc)
                            .radius(dis)
                            .keyword(query)
                            .pageNum(0));
                }else{
                    Toast.makeText(SearchActivity.this,"请输入要搜索的内容！",Toast.LENGTH_LONG).show();
                }
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                locationre = new double[2];
                locationre[0] = nearList.get(arg2).location.latitude;
                locationre[1] = nearList.get(arg2).location.longitude;
                locationName = nearList.get(arg2).name;
                onBackPressed();
            }
        });

        prv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(pageindex == 0){
                    Toast.makeText(SearchActivity.this,"已经是第一页了",Toast.LENGTH_LONG).show();
                }else if(pageindex != -1){
                    mPoiSearch.searchNearby(new PoiNearbySearchOption()
                            .location(loc)
                            .radius(dis)
                            .keyword(searchInfo)
                            .pageNum(--pageindex));
                }
            }
        });

        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(pageindex == pagenum-1){
                    Toast.makeText(SearchActivity.this,"已经是最后一页了",Toast.LENGTH_LONG).show();
                }else if(pageindex != -1){
                    mPoiSearch.searchNearby(new PoiNearbySearchOption()
                            .location(loc)
                            .radius(dis)
                            .keyword(searchInfo)
                            .pageNum(++pageindex));
                }
            }
        });

    }

    OnGetPoiSearchResultListener listener = new OnGetPoiSearchResultListener() {
        @Override
        public void onGetPoiResult(PoiResult poiResult) {
            if (poiResult != null && poiResult.getTotalPoiNum() != 0 ) {
                nearList = poiResult.getAllPoi();
                adapter = new LocNearAddressAdapter(SearchActivity.this, nearList);
                mListView.setAdapter(adapter);
                if(poiResult.getTotalPageNum()>2){
                    pagenum = poiResult.getTotalPageNum()-2;
                }else{
                    pagenum = poiResult.getTotalPageNum();
                }
                page.setText((pageindex+1)+"/"+pagenum);
            }else{
                Toast.makeText(SearchActivity.this,"没有找到结果",Toast.LENGTH_LONG).show();
            }

        }
        @Override
        public void onGetPoiDetailResult(PoiDetailSearchResult poiDetailSearchResult) {

        }
        @Override
        public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {

        }
        //废弃
        @Override
        public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {

        }
    };

    @Override
    public void onBackPressed() {
        Intent intent = new Intent();
        intent.putExtra("loc",locationre);
        intent.putExtra("locName",locationName);
        setResult(RESULT_OK,intent);
        super.onBackPressed();
    }
}


